﻿using System;
using System.Data;
using ORM.Attribute;

namespace ORM
{
    public class TypeParseHelper
    {
        public static string ParseType(string fullName)
        {
            string result = "";
            switch (fullName)
            {
                case "System.Int32":
                    result = "int";
                    break;
                case "System.Int16":
                    result = "binyint";
                    break;
                case "System.String":
                    result = "varchar";
                    break;
                case "System.DateTime":
                    result = "DateTime";
                    break;
                case "System.Bool":
                    result = "bit";
                    break;
                default:
                    break;
            }
            return result;
        }

        public static SqlDbType ParseType(Type type)
        {
            SqlDbType dbType = SqlDbType.NVarChar;
            string typeName = type.Name.ToLower();
            switch (typeName)
            {
                case "string":
                    dbType = SqlDbType.VarChar;
                    break;
                case "int64":
                    dbType = SqlDbType.BigInt;
                    break;
                case "int32":
                    dbType = SqlDbType.Int;
                    break;
                case "int16":
                    dbType = SqlDbType.SmallInt;
                    break;
                case "decimal":
                    dbType = SqlDbType.Decimal;
                    break;
                case "float":
                    dbType = SqlDbType.Float;
                    break;
                case "datetime":
                    dbType = SqlDbType.DateTime;
                    break;
                case "money":
                    dbType = SqlDbType.Money;
                    break;
                default:
                    break;
            }
            return dbType;
        }

        public static SqlDbType ParseType(DbFieldType type)
        {
            SqlDbType dbType = SqlDbType.VarChar;
            switch (type)
            {
                case DbFieldType.INT:
                    dbType = SqlDbType.Int;
                    break;
                case DbFieldType.TINYINT:
                    dbType = SqlDbType.TinyInt;

                    break;
                case DbFieldType.VARCHAR:
                    dbType = SqlDbType.VarChar;

                    break;
                case DbFieldType.NVARCHAR:
                    dbType = SqlDbType.NVarChar;

                    break;
                case DbFieldType.DATETIME:
                    dbType = SqlDbType.DateTime;

                    break;
                case DbFieldType.BIT:
                    dbType = SqlDbType.Bit;

                    break;
                case DbFieldType.TEXT:
                    dbType = SqlDbType.Text;

                    break;
                case DbFieldType.NTEXT:
                    dbType = SqlDbType.NText;

                    break;
                case DbFieldType.XML:
                    dbType = SqlDbType.Xml;

                    break;
                case DbFieldType.MONEY:
                    dbType = SqlDbType.Money;

                    break;
                case DbFieldType.FLOAT:
                    dbType = SqlDbType.Float;

                    break;
                case DbFieldType.CHAR:
                    dbType = SqlDbType.Char;

                    break;
                case DbFieldType.NCHAR:
                    dbType = SqlDbType.NChar;

                    break;
                default:
                    break;
            }
            return dbType;
        }
    }
}
